#include "app_ch_set.h"
#include "app_settings.h"
#include "../hal/hal_link.h"
#include <stdio.h>

// 通道数量
#define CHANNEL_COUNT 10

// 全局变量，存储 通道校准 界面对象
static lv_obj_t *ch_set_screen;
static lv_style_t transparent_style;

static lv_obj_t * rocker[CHANNEL_COUNT];
static lv_obj_t * app_ch_dropdown[CHANNEL_COUNT];

// 返回设置界面按钮点击事件处理函数
static void back_button_event_cb(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    // hal_link->tx_mode(selet_mode);
    
    // 以淡入动画效果切换回设置界面
    lv_scr_load_anim(settings_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, 50, 0, false);
}

// 存储通道数据的数组
uint8_t channel_data[CHANNEL_COUNT];
uint8_t *channel_user_ch;   // 指向使用的通道地址，在linux的通信里面

// 下拉选择按钮事件处理函数
static void dropdown_event_cb(lv_event_t * e)
{
    lv_obj_t * dropdown = lv_event_get_target(e);
    uint16_t sel = lv_dropdown_get_selected(dropdown);

    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        uint8_t *index = (uint8_t *)lv_event_get_user_data(e);
        hal_link->tx_u8_2(LINUX_LINK_CH_SELETE, index[0], sel);
    }
}

// 取反按钮事件处理函数
static void invert_button_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        uint8_t *index = (uint8_t *)lv_event_get_user_data(e);
        hal_link->tx_u8(LINUX_LINK_REVERSE, index[0]);
    }
}

void set_remote_info_for_ch_set(uint8_t * rock, uint8_t *flag)
{
    if (!is_screen_active(ch_set_screen)) {
        return;
    }

    for (int i=0; i < CHANNEL_COUNT; i++) {
        // if (rock[i] <= 100) {
            lv_slider_set_value(rocker[i], rock[i], LV_ANIM_OFF);
            if (flag[i]) {
                lv_obj_set_style_bg_color(rocker[i], lv_palette_main(LV_PALETTE_BLUE), LV_PART_INDICATOR);
            } else {
                lv_obj_set_style_bg_color(rocker[i], lv_palette_main(LV_PALETTE_ORANGE), LV_PART_INDICATOR);
            }
        // }
    }
    
}

// 创建列表项目
static void create_list_item(lv_obj_t * list, int index)
{
    lv_obj_t * list_item = lv_list_add_btn(list, NULL, NULL);
    
    channel_data[index] = index;

    lv_obj_t * div = lv_obj_create(list_item);
    lv_obj_set_width(div, lv_pct(100));
    lv_obj_set_height(div, 70);
    lv_obj_clear_flag(div, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_style(div, &transparent_style, LV_STATE_DEFAULT);

    lv_obj_t *label = lv_label_create(div);

    // 构建要显示的文本
    char text[20];
    snprintf(text, sizeof(text), "CH%d", index);
    // 设置标签的文本
    lv_label_set_text(label, text);
    lv_obj_align(label, LV_ALIGN_LEFT_MID, 0, 0);

    lv_obj_t * label_state = lv_label_create(div);
    lv_label_set_text(label_state, "state:off");
    lv_obj_align_to(label_state, label, LV_ALIGN_OUT_RIGHT_MID, 20, 0);

    // 创建横向进度条
    rocker[index] = lv_bar_create(div);
    lv_slider_set_range(rocker[index], 0, 100);      /*添加范围*/
    lv_obj_set_size(rocker[index], 150, 20);
    lv_obj_align_to(rocker[index], label_state, LV_ALIGN_OUT_RIGHT_MID, 20, 4);
    
    // 创建取反按钮
    lv_obj_t * invert_button = lv_btn_create(div);
    lv_obj_set_size(invert_button, 100, 30);
    lv_obj_add_event_cb(invert_button, invert_button_event_cb, LV_EVENT_CLICKED, &channel_data[index]);
    lv_obj_t * label_bt = lv_label_create(invert_button);
    lv_label_set_text(label_bt, "Invert");
    lv_obj_align(label_bt, LV_ALIGN_CENTER, 0, 0);
    lv_obj_align_to(invert_button, rocker[index], LV_ALIGN_OUT_RIGHT_MID, 100, 0);

    // 创建下拉选择按钮
    app_ch_dropdown[index] = lv_dropdown_create(div);
    lv_dropdown_set_options(app_ch_dropdown[index], "channel 1\nchannel 2\nchannel 3\nchannel 4\nchannel 5");
    lv_obj_set_width(app_ch_dropdown[index], 120);
    lv_obj_set_height(app_ch_dropdown[index], 40);
    lv_obj_add_event_cb(app_ch_dropdown[index], dropdown_event_cb, LV_EVENT_VALUE_CHANGED, &channel_data[index]);
    lv_obj_align_to(app_ch_dropdown[index], invert_button, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
}

static void setup_ui(void)
{
    lv_style_init(&transparent_style);

    // 设置无边框
    lv_style_set_border_width(&transparent_style, 0);
    lv_style_set_border_color(&transparent_style, lv_color_hex(0x000000));
    lv_style_set_border_opa(&transparent_style, LV_OPA_TRANSP);

    // 创建列表
    lv_obj_t * list = lv_list_create(ch_set_screen);
    lv_obj_set_size(list, lv_pct(80), lv_pct(70));
    lv_obj_align(list, LV_ALIGN_CENTER, 0, 30);

    for (int i = 0; i < CHANNEL_COUNT; i++) {
        create_list_item(list, i);
    }
}

/**
 * @brief  创建主界面
 * @param  void
 * @retval void
 */
void ch_set_screen_init(void)
{
    ch_set_screen = lv_obj_create(NULL);

    show_return_base("Channel settings", ch_set_screen, back_button_event_cb);

    setup_ui();
}

void set_channel_user_ch_p(uint8_t *p)
{
    channel_user_ch = p;
}

// 获取 About OS 界面对象
lv_obj_t *ch_set_screen_get(void) 
{
    for (int i = 0; i < CHANNEL_COUNT; i++)
    {
        lv_dropdown_set_selected(app_ch_dropdown[i], channel_user_ch[i]);
    }
    
    return ch_set_screen;
}
/******************* (C) COPYRIGHT 2025 FUYEBING*****END OF FILE************/